<template>
  <div class="login_cont">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="logo_box">
        <img src="../assets/dlam.jpeg" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form
        ref="reset"
        :model="formsj"
        :rules="rules"
        label-width="0px"
        class="form_box"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="formsj.username"
            prefix-icon="el-icon-s-custom"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="formsj.password"
            prefix-icon="el-icon-lock"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="buts">
          <el-button type="success" plain @click="login">登录</el-button>
          <el-button type="info" plain @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 数据绑定对象
      formsj: {
        username: "admin",
        password: "123456",
      },
      //   表单验证规则
      rules: {
        //   用户名验证
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        // 密码验证
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //   对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
    reset() {
      this.$refs.reset.resetFields();
    },
    // 登录验证
    login() {
      this.$refs.reset.validate(async (item) => {
        //   判断验证是否通过
        if (!item) return;
        // 获取发起请求后获得的数据
        var { data: res } = await this.$http.post("login", this.formsj);
        // 登录失败
        if (res.meta.status !== 200) {
          return this.$message.error("登录失败");
        }
        this.$message({
          message: "登录成功",
          type: "success",
        });
        console.log(res);
        // 将token存在sessionStorage中
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>
// 让样式私有化(作用域)
<style lang="less" scoped>
.login_cont {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .logo_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    overflow: hidden;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .form_box {
    position: absolute;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
    padding: 20px;
    .buts {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>